package com.micromall.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.micromall.common.utils.Dict;
import com.micromall.model.BusiCart;
import com.micromall.mapper.BusiCartMapper;
import com.micromall.model.BusiCartDetailItem;
import com.micromall.service.IBusiCartDetailItemService;
import com.micromall.service.IBusiCartService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author Dante Fung
 * @since 2017-07-26
 */
@Service
public class BusiCartServiceImpl extends ServiceImpl<BusiCartMapper, BusiCart> implements IBusiCartService {

    @Autowired
    private IBusiCartDetailItemService busiCartDetailItemService;

    @Override
    public Boolean handleCartData(String catrId, String totalAmount, String[] ItemIds, String[] ItemINums) {
        boolean b = false;
        boolean B = false;
        EntityWrapper<BusiCart> ew = new EntityWrapper<>();
        BusiCart busiCart = new BusiCart();
        busiCart.setId(catrId);
        ew.setEntity(busiCart);
        busiCart = this.selectOne(ew);
        busiCart.setTotalAmount(totalAmount);
        ew.setEntity(busiCart);
        //b = this.update(busiCart,ew);
        b = this.updateById(busiCart);
        EntityWrapper<BusiCartDetailItem> ew1 = new EntityWrapper<>();
        BusiCartDetailItem busiCartDetailItem = new BusiCartDetailItem();
        busiCartDetailItem.setCartId(busiCart.getId());
        busiCartDetailItem.setStandy3(Dict.YesOrNo.Yes.getCode());
        busiCartDetailItem.setStandy4(Dict.BusiCaer.CartItemStatusCode.NEW.getCode());
        ew1.setEntity(busiCartDetailItem);
        List<BusiCartDetailItem> busiCartDetailItemList = busiCartDetailItemService.selectList(ew1);
        if(null != busiCartDetailItemList && busiCartDetailItemList.size() >0 ){
            for (BusiCartDetailItem item: busiCartDetailItemList) {
                String itemId =  item.getId();
                for (int i = 0; i < ItemIds.length; i++) {
                    String id = ItemIds[i];
                    if(id.equals(itemId)){
//                        item.setStandy4(Dict.BusiCaer.CartItemStatusCode.PREPARE.getCode());
                        item.setSum(ItemINums[i]);
                        B = busiCartDetailItemService.updateById(item);
                    }
                }
            }

        }
        return B;
    }
}
